<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="流水号" prop="monitorRecordNo" label-width="80px">
        <el-input v-model="queryParams.monitorRecordNo" placeholder="请输入患者流水号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="住院号" prop="patientNo">
        <el-input v-model="queryParams.patientNo" placeholder="请输入住院号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="患者姓名缩写" prop="patientNameAcronym" label-width="110px">
        <el-input v-model="queryParams.patientNameAcronym" placeholder="请输入患者姓名缩写" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="入组日期" prop="inputFormDate">
        <el-date-picker v-model="dateRange" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="提交状态" prop="submitStatus" label-width="80px">
        <el-select v-model="queryParams.submitStatus" @change="handleQuery" placeholder="请选择" clearable>
          <el-option label="暂存" value="0"></el-option>
          <el-option label="已提交" value="1"></el-option>
        </el-select>

      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" @change="handleQuery" placeholder="请选择" clearable>
          <el-option label="驳回" value="0"></el-option>
          <el-option label="通过" value="1"></el-option>
          <el-option label="未审核" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="导出状态" prop="exportStatus" label-width="110px">
        <el-select v-model="queryParams.exportStatus" @change="handleQuery" placeholder="请选择" clearable>
          <el-option value="0" label="未导出"></el-option>
          <el-option value="1" label="已导出"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="监测员" prop="inspectorSign">
        <el-input
          v-model="queryParams.inspectorSign"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <br />
      <el-form-item label="质控员" prop="qualityControllerSign">
        <el-input
          v-model="queryParams.qualityControllerSign"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      <el-button type="warning" size="mini" plain icon="el-icon-download" @click="handleExport">下载</el-button>
      <el-button type="warning" size="mini" plain @click="handleExportJSON">导出JSON</el-button>
    </el-form>

    <el-table border="" v-loading="loading" :data="managerInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" :selectable="handleDisabled" />
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="流水号" align="center" prop="monitorRecordNo"></el-table-column>
      <el-table-column label="患者姓名缩写" align="center" prop="patientNameAcronym"></el-table-column>
      <el-table-column label="入组日期" align="center" prop="inputFormDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inputFormDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="监测员" align="center" prop="inspectorSign" />
      <el-table-column label="质控员" align="center" prop="qualityControllerSign" />
      <el-table-column label="提交状态" align="center" prop="submitStatus" width="120">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.submitStatus == 0">暂存</el-tag>
          <el-tag type="success" v-if="scope.row.submitStatus == 1">已提交</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus" width="120">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.auditStatus == 0">驳回</el-tag>
          <el-tag type="success" v-if="scope.row.auditStatus == 1">通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="导出状态" align="center" prop="exportStatus" width="120">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.exportStatus == 1">已导出</el-tag>
          <el-tag type="info" v-else>未导出</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="驳回原因" align="center" prop="rejectReason" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" width="240">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleLook(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-download" :disabled="scope.row.auditStatus != 1"
            @click="handleDownload(scope.row)">下载</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改电子监测管理信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="表名称" prop="monitorTableType">
          <el-select style="width: 100%" v-model="form.monitorTableType" placeholder="请选择表类型">
            <el-option v-for="option in tableOptions" :key="option.value" :label="option.label"
              :value="option.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getInfo,
  downloadPDF,
  downjsons,
  delAAAA
} from "@/api/workstation/aFormManage";

export default {
  name: "ManagerInfo",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电子监测管理信息表格数据
      managerInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        medicalRecordNo: undefined,
        monitorId: undefined,
        monitorRecordNo: undefined,
        monitorTableType: undefined,
        inputFormDate: undefined,
        isInputForm: undefined,
        auditStatus: undefined,
        exportStatus: undefined,
        inspectorSign: undefined,
        qualityControllerSign: undefined,
        submitStatus: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "主键id不能为空", trigger: "blur" }],
        medicalRecordNo: [
          { required: true, message: "患者流水号不能为空", trigger: "blur" },
        ],
        monitorId: [{ required: true, message: "监测表id不能为空", trigger: "blur" }],
        monitorRecordNo: [
          { required: true, message: "监测表流水号不能为空", trigger: "blur" },
        ],
        monitorTableType: [
          { required: true, message: "表名称不能为空", trigger: "change" },
        ],
        inputFormDate: [{ required: true, message: "入组日期不能为空", trigger: "blur" }],
        isInputForm: [{ required: true, message: "是否已入组不能为空", trigger: "blur" }],
      },
      tableOptions: [
        { label: "A表 监测信息表", value: 0 }, // 前台显示 0A表 监测信息表，后台传递 0
        { label: "B表 药品不良反应/事件报告表", value: 1 }, // 前台显示 1B表 药品不良反应/事件报告表，后台传递 1
      ],
      // 日期范围
      dateRange: [],
    };
  },
  created() {
    this.getList();
  },
  computed: {
    role() {
      return this.$store.state.user.roles;
    },
    useInfo() {
      return this.$store.state.user.user;
    },
  },
  methods: {
    isDisabled(row) {
      if (row.inputFormDate && row.monitorRecordNo) {
        return false
      } else {
        return true
      }
    },
    handleDisabled(row, index) {
      return row.auditStatus == 1 && row.monitorRecordNo ? true : false;
    },
    /** 查询电子监测管理信息列表 */
    getList() {
      this.loading = true;

      getInfo(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
        this.managerInfoList = response.rows
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        medicalRecordNo: undefined,
        monitorId: undefined,
        monitorRecordNo: undefined,
        monitorTableType: undefined,
        inputFormDate: undefined,
        isInputForm: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增监测表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        path: "/electronicDetectionForm",
        query: {
          aId: row.id,
          medicalRecordNo: row.medicalRecordNo,
        },
      });
    },
    // 查看按钮
    handleLook(row) {
      this.$router.push({
        path: "/electronicDetectionForm",
        query: {
          aId: row.id,
          medicalRecordNo: row.medicalRecordNo,
          isLook: true,
        },
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.medicalRecordNo = this.$route.query.medicalRecordNo;
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateManagerInfo(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addManagerInfo(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(
          `是否确认删除患者姓名为 ${row.patientNameAcronym || '-'} 的A表监测信息表的数据项？`
        )
        .then(() => {
          this.loading = true;
          return delAAAA(ids);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { })
        .finally(() => {
          this.loading = false;
        });
    },
    // 下载
    async handleDownload(row) {
      this.$download.get(
        `/workstation/monitorOneInfo/download/${row.id}`,
        '',
        `检测表_${new Date().getTime()}.zip`
      );
    },
    async handleExportJSON(row) {
      if (this.ids.length == 0) return this.$modal.msgError("请选择要导出的数据");
      this.$download.get(
        `/workstation/managerInfo/json/${this.ids}`,
        '',
        `A表JSON_${new Date().getTime()}.zip`
      );
    },
    /** 导出按钮操作 */
    handleExport() {
      if (this.ids.length == 0) return this.$modal.msgError("请选择要导出的数据");
      this.$download.get(
        `/workstation/monitorOneInfo/download/${this.ids}`,
        '',
        `监测表_${new Date().getTime()}.zip`
      );
    },
  },
};
</script>
<style scoped>
::v-deep .el-form-item__label-wrap {
  margin-left: 0 !important;
}
</style>
